﻿// ------------------------------------------------------------------------
//    XBMControl - A compact remote controller for XBMC (.NET 3.5)
//    Copyright (C) 2008  Bram van Oploo (bramvano@gmail.com)
//                        Mike Thiels (Mike.Thiels@gmail.com)
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
// ------------------------------------------------------------------------

using System;
using System.Windows.Forms;

namespace XBMC
{
    public class XbmcStatus
    {
        readonly XbmcCommunicator _parent;

        //XBMC Properties
        private bool _isConnected;
        private bool _isPlaying;
        private bool _isNotPlaying = true;
        private bool _isPlayingLastFm;
        private bool _isPaused;
        private bool _isMuted;
        private int _volume;
        private int _progress = 1;
        private string _mediaNowPlaying;
        private bool _newMediaPlaying = true;
        private readonly Timer _heartBeatTimer;
        private const int ConnectedInterval = 5000;
        private const int DisconnectedInterval = 10000;

        public XbmcStatus(XbmcCommunicator p)
        {
            _parent = p;
            _heartBeatTimer = new Timer {Interval = ConnectedInterval};
            _heartBeatTimer.Tick += HeartBeat_Tick;
        }

        public void Refresh()
        {
            if (!_isConnected) return;
            if (_mediaNowPlaying != _parent.NowPlaying.Get("filename", true) || _mediaNowPlaying == null)
            {
                _mediaNowPlaying = _parent.NowPlaying.Get("filename");
                _newMediaPlaying = true;
            }
            else
                _newMediaPlaying = false;

            _isPlaying = (_parent.NowPlaying.Get("playstatus", true) == "Playing") ? true : false;
            _isPaused = (_parent.NowPlaying.Get("playstatus", true) == "Paused") ? true : false;
            _isNotPlaying = (_mediaNowPlaying == "[Nothing Playing]" || _mediaNowPlaying == null) ? true : false;

            if (_mediaNowPlaying == null || _isNotPlaying || _mediaNowPlaying.Length < 6)
            {
                _isPlayingLastFm = false;
            }
            else
            {
                _isPlayingLastFm = (_mediaNowPlaying.Substring(0, 6) == "lastfm") ? true : false;
            }

            var aVolume = _parent.Request("GetVolume");
            var aProgress = _parent.Request("GetPercentage");

            if (aVolume == null || aVolume[0] == "Error")
                _volume = 0;
            else
                _volume = Convert.ToInt32(aVolume[0]);

            if (aProgress == null || aProgress[0] == "Error" || aProgress[0] == "0" || Convert.ToInt32(aProgress[0]) > 99)
                _progress = 1;
            else
                _progress = Convert.ToInt32(aProgress[0]);

            _isMuted = (_volume == 0) ? true : false;
        }

        private void HeartBeat_Tick(object sender, EventArgs e)
        {
            _isConnected = _parent.SetResponseFormat();
            _heartBeatTimer.Interval = (_isConnected) ? ConnectedInterval : DisconnectedInterval;
        }

        public bool IsConnected()
        {
            return _isConnected;
        }

        public void EnableHeartBeat()
        {
            HeartBeat_Tick(null, null);
            _heartBeatTimer.Enabled = true;
        }

        public void DisableHeartBeat()
        {
            _heartBeatTimer.Enabled = false;
        }

        public bool WebServerEnabled()
        {
            var webserverEnabled = _parent.Request("WebServerStatus");

            if (webserverEnabled == null)
                return false;
            return (webserverEnabled[0] == "On") ? true : false;
        }

        public bool IsNewMediaPlaying()
        {
            return _newMediaPlaying;
        }

        public bool IsPlaying(string lastfm)
        {
            return (lastfm != null) ? _isPlayingLastFm : _isPlaying;
        }

        public bool IsPlaying()
        {
            return IsPlaying(null);
        }

        public bool IsNotPlaying()
        {
            return _isNotPlaying;
        }

        public bool IsPaused()
        {
            return _isPaused;
        }

        public bool IsMuted()
        {
            return _isMuted;
        }

        public int GetVolume()
        {
            return _volume;
        }

        public int GetProgress()
        {
            return _progress;
        }

        public bool LastFmEnabled()
        {
            var aLastFmUsername = _parent.Request("GetGuiSetting(3;lastfm.username)");
            var aLastFmPassword = _parent.Request("GetGuiSetting(3;lastfm.password)");

            if (aLastFmUsername == null || aLastFmPassword == null)
                return false;
            return (aLastFmUsername[0] == "" || aLastFmPassword[0] == "") ? false : true;
        }

        public bool RepeatEnabled()
        {
            var aRepeatEnabled = _parent.Request("GetGuiSetting(1;musicfiles.repeat)");
            if (aRepeatEnabled == null)
                return false;
            return (aRepeatEnabled[0] == "False") ? false : true;
        }
    }
}
